<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar
      left-arrow
      title="编辑资料"
      @click-left="$router.back()"
    ></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image
        width="120"
        height="120"
        fit="cover"
        round
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        :value="userInfo.name"
        @click="editNameClick"
      />
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="
          DatetimePicker = true;
          currentDate = new Date(userInfo.birthday);
        "
      />
    </van-cell-group>

    <!-- 编辑姓名弹窗 -->
    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
      @confirm="updateName"
    >
      <van-field ref="input" v-model="newName" placeholder="请输入姓名" />
    </van-dialog>

    <!-- 编辑性别弹窗 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="updateGender(0)" />
      <van-cell title="女" is-link @click="updateGender(1)" />
    </van-popup>

    <!-- 修改生日  -->
    <van-popup v-model="DatetimePicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="DatetimePicker = false"
        @confirm="updateBirthday"
      />
    </van-popup>
    <!-- 图片裁切 -->
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="updatePhoto">确认</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { editUserInfo, editUserPhoto } from "./user.js";
import moment from "moment";
//导入vue-cropper裁切组件
import { VueCropper } from "vue-cropper";

export default {
  name: "edit",
  components: {
    VueCropper,
  },
  data() {
    return {
      showDialog: false,
      newName: "",
      showPopup: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2030, 10, 1),
      currentDate: new Date(),
      DatetimePicker: false, //编辑生日弹窗
      img: "", //要裁切的图片
      showCropper: false, //裁切盒子显示/隐藏
    };
  },
  methods: {
    //点击修改名字按钮
    editNameClick() {
      this.showDialog = true;
      this.newName = this.userInfo.name;
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    //修改名字
    updateName() {
      //调用接口
      editUserInfo({ name: this.newName }).then((res) => {
        console.log(res);
        //弹窗提示
        this.$toast.success("更新成功");
        //更新vuex
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          name: this.newName,
        });
      });
    },
    //修改性别
    updateGender(gender) {
      editUserInfo({ gender }).then((res) => {
        //弹窗提示
        this.$toast.success("更新成功");
        this.showPopup = false;
        //更新vuex
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          gender,
        });
      });
    },
    //修改生日
    updateBirthday() {
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      editUserInfo({ birthday }).then((res) => {
        //关闭弹窗
        this.DatetimePicker = false;
        //弹窗提示
        this.$toast.success("更新成功!");
        //更新vuex数据
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          birthday,
        });
      });
    },
    //选择文件
    afterRead(file) {
      console.log(file);
      // console.log(file.content);
      console.log(file.file); //文件对象
      console.log(URL.createObjectURL(file.file));
      //1.设置裁切图片的路径(cropper组件不支持file对象， 但是支持base64)
      this.img = file.content;
      //2.显示裁切盒子
      this.showCropper = true;
    },
    //上传裁切后的图片
    updatePhoto() {
      //1.获取裁切之后的图片（组件默认CV的时候就加了ref="cropper")
      this.$refs.cropper.getCropBlob((data) => {
        //生成formmdata对象
        let fd = new FormData();
        fd.append("photo", data);
        //提交文件到服务器
        editUserPhoto(fd).then((res) => {
          console.log(res);
          //弹窗提示
          this.$toast.success("更新成功!");
          //隐藏裁切框
          this.showCropper = false;
          //更新vuex数据
          this.$store.commit("updateUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
        });
      });
    },
  },
  computed: {
    //添加计算属性
    ...mapState(["userInfo"]),
  },
};
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  .van-nav-bar {
    background-color: #96ccf0;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像部分
  .avatar {
    display: flex;
    height: 150px;
    justify-content: center;
    align-items: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
    /* 文件上传盒子 */
    .van-uploader {
      position: absolute;
      opacity: 0;
      width: 120px;
      height: 120px;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
  /* 裁切盒子 */
  .cropper {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
